package repository

import (
	"errors"

	"gitee.com/ysl729895/go-mall/internal/model"
	"gorm.io/gorm"
)

// CategoryRepository 定义分类数据操作接口
//go:generate mockgen -source=category_repo.go -destination=mock_category_repo.go -package=repository

type CategoryRepository interface {
	Create(category *model.Category) error
	GetAll() ([]model.Category, error)
}

type categoryRepository struct {
	db *gorm.DB
}

// NewCategoryRepository 构造函数
func NewCategoryRepository(db *gorm.DB) CategoryRepository {
	return &categoryRepository{db: db}
}

// ErrCategoryNotFound 分类未找到错误
var ErrCategoryNotFound = errors.New("分类未找到")

// Create 创建新分类
func (r *categoryRepository) Create(category *model.Category) error {
	return r.db.Create(category).Error
}

// GetAll 获取所有分类
func (r *categoryRepository) GetAll() ([]model.Category, error) {
	var categories []model.Category
	err := r.db.Find(&categories).Error
	return categories, err
}
